/**
 * Octopus, an XMMS2 client.
 * Copyright (C) 2014 Octopus Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace OctopusUi {
	public class UiMedialibPanel : Mx.Frame {
		public UiBrowsePanel browsePanel;
		public Mx.Frame tracklistPanelFrame;
		public IKeyFocusActor keyFocusActor;

		public bool use_texture_cache { get; set; default = false; }

		public signal void load_finished ();
		public signal void key_focus ();
		public signal void show_message_bar_text (string message);

		public UiMedialibPanel () {
			var medialibPanelStack = new Mx.Stack ();
			medialibPanelStack.name = "medialibPanel-background";
			medialibPanelStack.x_align = Clutter.ActorAlign.FILL;
			medialibPanelStack.y_align = Clutter.ActorAlign.FILL;
			this.add_child (medialibPanelStack);

			//browsePanel
			var browsePanelFrame = new Mx.Frame ();
			browsePanelFrame.x_align = Clutter.ActorAlign.FILL;
			browsePanelFrame.y_align = Clutter.ActorAlign.FILL;
			medialibPanelStack.add_child (browsePanelFrame); 
			this.browsePanel = new UiBrowsePanel ();
			this.browsePanel.use_texture_cache = this.use_texture_cache;
			if (Octopus.Global.get ().get_configure ().has_key ("ui", "layout")) {
				switch (Octopus.Global.get ().get_configure ().get_integer("ui", "layout")) {
					case LayoutEnum.GRID:
						this.browsePanel.layout = LayoutEnum.GRID;
						break;
					case LayoutEnum.DEFORMED:
						this.browsePanel.layout = LayoutEnum.DEFORMED;
						break;
				}
			}
			browsePanelFrame.add_child (this.browsePanel);
			this.keyFocusActor = this.browsePanel;
			//tracklistPanel
			this.tracklistPanelFrame = new Mx.Frame ();
			this.tracklistPanelFrame.x_align = Clutter.ActorAlign.FILL;
			this.tracklistPanelFrame.y_align = Clutter.ActorAlign.FILL;
			medialibPanelStack.add_child (this.tracklistPanelFrame);

			var medialibPanelOverlay = new Mx.Frame ();
			medialibPanelOverlay.name = "medialibPanel-overlay";
			medialibPanelStack.add_child (medialibPanelOverlay);

			//connect to signals
			this.browsePanel.load_finished.connect (() => {
				load_finished ();
			});
			this.browsePanel.key_focus.connect (() => {
				this.keyFocusActor = this.browsePanel;
				key_focus ();
			});
			this.browsePanel.tracklist_panel_show.connect((/*Octopus.IMedialibInfo*/ album_model) => {
				this.browsePanel.desaturateEffect.enabled = true;
				var tracklistPanel = new UiTracklistPanel (album_model);
				tracklistPanel.use_texture_cache = this.use_texture_cache;
				this.tracklistPanelFrame.add_child (tracklistPanel);
				tracklistPanel.tracklist_panel_close.connect (() => {
					this.browsePanel.desaturateEffect.enabled = false;
					tracklistPanel.destroy ();
					this.keyFocusActor = this.browsePanel;
					key_focus ();
				});
				tracklistPanel.key_focus.connect (() => {
					this.keyFocusActor = tracklistPanel;
					key_focus ();
				});
				tracklistPanel.show_message_bar_text.connect ((message) => {
					show_message_bar_text (message);
				});
				tracklistPanel.tracklistPanelShow ();
			});
			this.browsePanel.show_message_bar_text.connect((message) => {
				show_message_bar_text (message);
			});
		}

		public void load () {
			this.browsePanel.load ();
		}
	}
}
